/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.addressbook.gui.utils;

import cz.muni.fi.pv168.addressbook.gui.MainWindow;
import java.awt.Component;
import java.util.ResourceBundle;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIDefaults;

import static java.util.ResourceBundle.getBundle;
import static javax.swing.JOptionPane.showConfirmDialog;
import static javax.swing.JOptionPane.showInputDialog;
import static javax.swing.JOptionPane.showMessageDialog;
import static javax.swing.UIManager.getDefaults;

/**
 *
 * @author Michal Ďuriš
 */
public class JOptionDialogWindowHandler {

    public JOptionDialogWindowHandler() {
    }

    public static void showErrorDialogWindow(Component parent, String title,
                                             String errorMessage) {

        showMessageDialog(parent, errorMessage, title, JOptionPane.ERROR_MESSAGE);
    }

    public static void showInfoDialogWindow(Component parent, String title,
                                            String infoMessage) {

        showMessageDialog(parent, infoMessage, title,
                JOptionPane.INFORMATION_MESSAGE);
    }

    public static int showYesNoDialogWindow(Component parent, String title,
                                            String questionMessage) {

        setDefaultButtonTextInLocaleLanguage();

        return showConfirmDialog(parent, questionMessage, title,
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    }

    public static Object showSelectBoxDialogWindow(Component parent,
                                                   String title,
                                                   String questionMessage,
                                                   Object[] selectionValues) {

        setDefaultButtonTextInLocaleLanguage();

        return showInputDialog(parent, questionMessage, title,
                JOptionPane.QUESTION_MESSAGE, null, selectionValues,
                selectionValues[0]);
    }

    public static String showInputDialogWindow(Component parent, String title,
                                               String inputDialogMessage,
                                               String input, ImageIcon icon) {

        setDefaultButtonTextInLocaleLanguage();

        return (String) showInputDialog(parent, inputDialogMessage, title,
                JOptionPane.QUESTION_MESSAGE, icon, null, input);
    }

    private static void setDefaultButtonTextInLocaleLanguage() {

        ResourceBundle bundle = getBundle(MainWindow.BUNDLE_PATH);

        Object[] optionPanelLanguageSettings = {
            "OptionPane.cancelButtonText", bundle.getString(
            "OptionMessage.Button.cancel"),
            "OptionPane.noButtonText", bundle.getString(
            "OptionMessage.Button.no"),
            "OptionPane.okButtonText", bundle.getString(
            "OptionMessage.Button.ok"),
            "OptionPane.yesButtonText", bundle.getString(
            "OptionMessage.Button.yes")};

        UIDefaults defaults = getDefaults();
        defaults.putDefaults(optionPanelLanguageSettings);
    }

}
